﻿@model ManufacturerListModel
@{   
    ViewBag.Title = T("Admin.Catalog.Manufacturers").Text;
}
<div class="section-header">
    <div class="title">
        <i class="far fa-building"></i>
        @T("Admin.Catalog.Manufacturers")
    </div>
    <div class="options">
		@{ Html.RenderWidget("admin_button_toolbar_before"); }

        <a href="@Url.Action("Create")" class="btn btn-primary">
			<i class="fa fa-plus"></i>
			<span>@T("Admin.Common.AddNew")</span>
		</a>

		@{ Html.RenderWidget("admin_button_toolbar_after"); }
    </div>
</div>

<div class="row mt-3 grid-filter">
	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		@Html.SmartLabelFor(model => model.SearchManufacturerName)
		@Html.TextBoxFor(model => Model.SearchManufacturerName, new { @class = "form-control" })
	</div>
	@if (!Model.IsSingleStoreMode)
	{
		<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
			@Html.SmartLabelFor(model => model.SearchStoreId)
			@Html.EditorFor(model => model.SearchStoreId)
		</div>
	}
	<div class="col-sm-6 col-lg-4 col-xl-3 form-group">
		@Html.SmartLabel(string.Empty, string.Empty)
		<button type="submit" id="btnSearch" value="btnSearch" class="btn btn-secondary btn-block">
			<span>@T("Admin.Common.ApplyFilter")</span>
		</button>
	</div>
</div>

<div class="mt-3">
	@(Html.Telerik().Grid<ManufacturerModel>()
		.Name("manufacturers-grid")
		.ClientEvents(events => events
			.OnDataBinding("onDataBinding")
			.OnDataBound("onDataBound"))
		.Columns(columns =>
		{
			columns.Bound(x => x.Id)
				.ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= Id #>' class='checkboxGroups'/>")
				.Title("<input id='mastercheckbox' type='checkbox'/>")
				.Width(50)
				.HtmlAttributes(new { style = "text-align:center" })
				.HeaderHtmlAttributes(new { style = "text-align:center" });
			columns.Bound(x => x.Name)
				.ClientTemplate("<a href=\"Edit/<#= Id #>\"><#= Name #></a>");
			columns.Bound(x => x.Published)
				.Width(160)
				.ClientTemplate(@Html.SymbolForBool("Published"))
				.Centered();
			columns.Bound(x => x.DisplayOrder)
				.Width(160)
				.Centered();
			columns.Bound(x => x.LimitedToStores)
				.Width(160)
				.ClientTemplate(@Html.SymbolForBool("LimitedToStores"))
				.Hidden(Model.IsSingleStoreMode)
				.Centered();
		})
		.Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
		.DataBinding(dataBinding => dataBinding.Ajax().Select("List", "Manufacturer"))
		.PreserveGridState()
		.EnableCustomBinding(true))
</div>

<script type="text/javascript">
	@*used by widget SmartStore.CommonExportProviders::ExportButtonsGeneral.cshtml*@
	var selectedIds = [];

	$(document).ready(function () {
		//search button
		$('#btnSearch').click(function () {
			//search
			var grid = $('#manufacturers-grid').data('tGrid');
			grid.currentPage = 1; //new search. Set page size to 1
			grid.ajaxRequest();
			return false;
		});

		$("#@Html.FieldIdFor(model => model.SearchManufacturerName)").keydown(function (event) {
			if (event.keyCode == 13) {
				$("#btnSearch").click();
				return false;
			}
		});

		$('#mastercheckbox').click(function () {
			$('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
		});

		//wire up checkboxes.
		$(document).on('change', '#manufacturers-grid input[type=checkbox][id!=mastercheckbox]', function (e) {
			var $check = $(this);
			if ($check.is(":checked") == true) {
				var checked = jQuery.inArray($check.val(), selectedIds);
				if (checked == -1) {
					selectedIds.push($check.val());
				}
			}
			else {
				var checked = jQuery.inArray($check.val(), selectedIds);
				if (checked > -1) {
					selectedIds = $.grep(selectedIds, function (item, index) {
						return item != $check.val();
					});
				}
			}
			updateMasterCheckbox();
		});
	});

	function onDataBinding(e) {
		var searchModel = {
			SearchManufacturerName: $('#@Html.FieldIdFor(model => model.SearchManufacturerName)').val(),
			SearchStoreId: $('#@Html.FieldIdFor(model => model.SearchStoreId)').val()
		};
		e.data = searchModel;
	}

	function onDataBound(e) {
		$('#manufacturers-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
			var currentId = $(this).val();
			var checked = jQuery.inArray(currentId, selectedIds);
			$(this).attr('checked', checked > -1);
		});

		updateMasterCheckbox();
	}

	function updateMasterCheckbox() {
		var numChkBoxes = $('#manufacturers-grid input[type=checkbox][id!=mastercheckbox]').length;
		var numChkBoxesChecked = $('#manufacturers-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
		$('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
	}

</script>